package com.example.baserecyclerviewadapterdemo.adapter.node.tree;

import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import androidx.core.view.ViewCompat;

import com.chad.library.adapter.base.BaseNodeAdapter;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.entity.node.tree.FirstNode;
import com.example.baserecyclerviewadapterdemo.entity.node.tree.SecondNode;
import com.example.baserecyclerviewadapterdemo.entity.node.tree.ThirdNode;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class NodeTreeAdapter extends BaseNodeAdapter {
    public static final int EXPAND_COLLAPSE_PAYLOAD = 110;
    public NodeTreeAdapter() {
        super();
        addNodeProvider(new FirstNodeProvider());
        addNodeProvider(new SecondNodeProvider());
        addNodeProvider(new ThirdNodeProvider());
    }

    @Override
    protected int getItemType(@NotNull List<? extends BaseNode> list, int i) {
        BaseNode baseNode = list.get(i);
        if (baseNode instanceof FirstNode) {
            return 1;
        }else if(baseNode instanceof SecondNode){
            return 2;
        }else if(baseNode instanceof ThirdNode){
            return 3;
        }
        return -1;
    }

    private class FirstNodeProvider extends BaseNodeProvider {
        @Override
        public int getItemViewType() {
            return 1;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_node_first;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, BaseNode baseNode) {
            FirstNode firstNode = (FirstNode) baseNode;
            holder.setText(R.id.tv,firstNode.getTitle());
            holder.setImageResource(R.id.iv,R.mipmap.arrow_r);
            setArrowSpan(holder,baseNode,false);
        }

        @Override
        public void convert(@NotNull BaseViewHolder helper, BaseNode item, @NotNull List<?> payloads) {
            super.convert(helper, item, payloads);
            for (Object payload : payloads) {
                if (payload instanceof Integer && (int)payload == EXPAND_COLLAPSE_PAYLOAD) {
                    setArrowSpan(helper,item,true);
                }
            }
        }

        @Override
        public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
            super.onClick(helper, view, data, position);
            getAdapter().expandOrCollapse(position,true,true,NodeTreeAdapter.EXPAND_COLLAPSE_PAYLOAD);
        }

        private void setArrowSpan(BaseViewHolder holder, BaseNode data, boolean isAnimation){
            FirstNode firstNode = (FirstNode) data;
            ImageView imageView = holder.getView(R.id.iv);
            if (firstNode.isExpanded()) {
                if (isAnimation) {
                    ViewCompat.animate(imageView)
                            .setDuration(200)
                            .setInterpolator(new DecelerateInterpolator())
                            .rotation(0f)
                            .start();
                }else{
                    imageView.setRotation(0f);
                }
            }else{
                if (isAnimation) {
                    ViewCompat.animate(imageView)
                            .setDuration(200)
                            .setInterpolator(new DecelerateInterpolator())
                            .rotation(90f)
                            .start();
                }else{
                    imageView.setRotation(90f);
                }
            }
        }
    }

    private class SecondNodeProvider extends BaseNodeProvider {
        @Override
        public int getItemViewType() {
            return 2;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_node_second;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, BaseNode baseNode) {
            SecondNode secondNode = (SecondNode) baseNode;
            holder.setText(R.id.title,secondNode.getTitle());
            if (secondNode.isExpanded()) {
                holder.setImageResource(R.id.iv,R.mipmap.arrow_b);
            }else{
                holder.setImageResource(R.id.iv,R.mipmap.arrow_r);
            }
        }

        @Override
        public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
            super.onClick(helper, view, data, position);
            SecondNode secondNode = (SecondNode) data;
            if (secondNode.isExpanded()) {
                getAdapter().collapse(position);
            }else{
                getAdapter().expandAndCollapseOther(position);
            }

        }
    }

    private class ThirdNodeProvider extends BaseNodeProvider {
        @Override
        public int getItemViewType() {
            return 3;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_node_third;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode) {
            ThirdNode thirdNode = (ThirdNode) baseNode;
            baseViewHolder.setText(R.id.title,thirdNode.getTitle());

        }
    }
}
